package chapter5.homework.heima.Q2ReceiveAndFeedback.mine;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Project ：learn_java
 * @File ：Server.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/3/13 17:24
 * @Description ：接收数据并打印，再给客户端反馈信息
 * 向外发送数据用bw，write，接收用br，reader
 */

public class Server {
    public static void main(String[] args) throws IOException {
        // 绑定端口
        ServerSocket ss = new ServerSocket(10000);

        // 监听连接
        Socket socket = ss.accept();

        // 获取输入流，用于接收客户端数据
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        // 获取输出流，用于给客户端发送反馈信息
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        // 读取数据
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            // 给客户端发送反馈信息
            bw.write("服务器已收到消息：" + line);
            bw.newLine();
            bw.flush();
        }

        // 释放资源
        bw.close();
        br.close();
        socket.close();
        ss.close();
    }
}
